#该脚本实现将image图片转为CSV文件
#产生fer2022文件
import cv2
import os
import numpy as np
import pandas as pd
np.set_printoptions(threshold=np.nan)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
import csv
def get_imlist(path):
    return [os.path.join(path,f) for f in os.listdir(path) if f.endswith('.jpg')]
   # 图像个数
# 遍历每张图片
#新的数据集命名为了fer2022,分别打开每个数据集进行编写，首先train部分:

with open("fer2022.csv", 'w',newline='') as f:
    f.write('emotion,pixel,Usage\n')
    c = get_imlist(r"train/Tired/")
    d = len(c)
    for i in range(d):
        img = cv2.imread(c[i],cv2.IMREAD_GRAYSCALE)  # 打开图像
        img_ndarray = np.asarray(img)  # 将图像转化为数组并将像素转化到0-1之间
        data = cv2.resize(img_ndarray,(48,48))#图像大小48*48像素值
        data = data.reshape(-1)
        s=data.shape
        csv_writer = csv.writer(f)
        pixels = (" ".join(str(s) for s in data))
        csv_writer.writerow(['0',pixels,"train"])
    c = get_imlist(r"train/Neutral/")
    d = len(c)
    for i in range(d):
        img = cv2.imread(c[i], cv2.IMREAD_GRAYSCALE)  # 打开图像
        img_ndarray = np.asarray(img)  # 将图像转化为数组并将像素转化到0-1之间
        # noinspection PyRedeclaration
        data = cv2.resize(img_ndarray, (48, 48))
        data = data.reshape(-1)
        csv_writer = csv.writer(f)
        s = data.shape
        pixels = (" ".join(str(s) for s in data))
        csv_writer.writerow(['1', pixels, "train"])
    c = get_imlist(r"train/Happy/")
    d = len(c)
    for i in range(d):
        img = cv2.imread(c[i], cv2.IMREAD_GRAYSCALE)  #打开图像
        img_ndarray = np.asarray(img)  # 将图像转化为数组并将像素转化到0-1之间

        data = cv2.resize(img_ndarray, (48, 48))
        data = data.reshape(-1)
        s = data.shape
        csv_writer = csv.writer(f)
        pixels = (" ".join(str(s) for s in data))
        csv_writer.writerow(['2', pixels, "train"])

    #验证集部分照片处理：     
    c = get_imlist(r"val/Tired/")
    d = len(c)
    for i in range(d):
        img = cv2.imread(c[i], cv2.IMREAD_GRAYSCALE)  # 打开图像
        img_ndarray = np.asarray(img)  # 将图像转化为数组并将像素转化到0-1之间
        data = cv2.resize(img_ndarray, (48, 48))
        data = data.reshape(-1)
        s = data.shape
        csv_writer = csv.writer(f)
        pixels = (" ".join(str(s) for s in data))
        csv_writer.writerow(['0', pixels, "val"])
    c = get_imlist(r"val/Neutral/")
    d = len(c)
    for i in range(d):
        img = cv2.imread(c[i], cv2.IMREAD_GRAYSCALE)  # 打开图像
        img_ndarray = np.asarray(img)  # 将图像转化为数组并将像素转化到0-1之间
        # noinspection PyRedeclaration
        data = cv2.resize(img_ndarray, (48, 48))
        data = data.reshape(-1)
        s = data.shape
        csv_writer = csv.writer(f)
        pixels = (" ".join(str(s) for s in data))
        csv_writer.writerow(['1', pixels, "val"])
    c = get_imlist(r"val/Happy/")
    d = len(c)
    for i in range(d):
        img = cv2.imread(c[i], cv2.IMREAD_GRAYSCALE)  # 打开图像
        img_ndarray = np.asarray(img)  # 将图像转化为数组并将像素转化到0-1之间
        # noinspection PyRedeclaration
        data = cv2.resize(img_ndarray, (48, 48))
        data = data.reshape(-1)
        s = data.shape
        csv_writer = csv.writer(f)
        pixels = (" ".join(str(s) for s in data))
        csv_writer.writerow(['2', pixels, "val"])
f.close()